﻿(function () {
    $(function () {
        var _riskPointService = abp.services.app.riskPoint;
        var _$modal = $("#RiskPointCreateModal");
        var _$form = _$modal.find("form");

        //新增保存
        _$form.find('button[type=submit]').click(function (e) {
            e.preventDefault();

            var organizationunitid = $("#OrganizationUnitId").val();
           

            if (!_$form.valid()) {
                return;
            }
            if (organizationunitid === "") {
                abp.message.warn("责任部门不能为空","请选择部门");
                return;
            }

          
            if ($("#OrganizationUnitId").val === "") {
                abp.message.warn("责任部门不能为空", "请选择责任部门");
                return;
            }
            var riskpoint = _$form.serializeFormToObject();

            //事故类型
            var strlist = "";
            //riskpoint.AccidentTypes = [];
            var _$roleCheckboxes = $("input[name='accidentlist']:checked");
            if (_$roleCheckboxes) {
                for (var roleIndex = 0; roleIndex < _$roleCheckboxes.length; roleIndex++) {
                    var _$roleCheckbox = $(_$roleCheckboxes[roleIndex]);
                                      strlist += _$roleCheckbox.val() + " ";
                }
            }
            if ($.trim(strlist) === "") {
                alert("请选择事故类型");
                return;
            }
            riskpoint.AccidentTypes =$.trim(strlist);
            //提交
            abp.ui.setBusy(_$modal);           
            _riskPointService.createOrUpdateRiskPoint({ riskpoint }).done(function () {
                _$modal.modal('hide');
                location.reload(true); //reload page to see edited user!
            }).always(function () {
                abp.ui.clearBusy(_$modal);
            });
        });


        //刷新页面
        $("#RefreshButton").click(function () {
            refreshpost();
        });

        function refreshpost() {
            location.reload();
        }

        //删除

        $(".delete-riskpoint").click(function () {


            var riskpointId = $(this).attr("data-riskpoint-id");
            var riskpointName = $(this).attr("data-riskpoint-name");
            
            deleteHazardSource(riskpointId, riskpointName);
        });

        function deleteHazardSource(hazardsourceId, hazardsourceName) {

            abp.message.confirm(
                abp.utils.formatString(abp.localization.localize('AreYouSureWantToDelete', 'DualControl'), hazardsourceName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _riskPointService.deleteRiskPoint({
                            id: hazardsourceId
                        }).done(function () {
                            refreshpost();
                        });
                    }
                }
            );
        }

        //编辑风险点
        $(".edit-riskpoint").click(function (e) {
            e.preventDefault();
            var riskpointId = $(this).attr("data-riskpoint-id"); 

            $.ajax({
                url: abp.appPath + 'RiskPoint/EditRiskPointModal?riskpointId=' + riskpointId,
                type: 'POST',
                contentType: 'application/html',
                success: function (content) {
                    $('#RiskPointEditModal div.modal-content').html(content);
                },
                error: function (e) { }
            });
        });

        //模态框关闭时，清除其中的内容。
        _$modal.on("hide.bs.modal", function () {
            _$form[0].reset();
        });
    });
})();